import psutil
import socket
import sys
from hook_socket import isrun


if isrun():
    run_my = True
else:
    run_my = False



import wx
import wx._xml
import wx.adv
import wx.richtext
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget,QVBoxLayout,QLabel,QApplication,QProgressBar,QMessageBox
from PyQt5.QtCore import QThread,QPropertyAnimation,pyqtSignal,QRect
import lzma
import websocket
import random
import PyHook3
import pythoncom
import win32api
import win32gui
import win32con
import json
import configparser
import os
import sys
import re

import threading
from io import BytesIO
import time
import subprocess
from multiprocessing import Process,Queue,freeze_support
import images

import wx_window
import hook_socket
import qt_window
import setting

TextStyle = """
QMessageBox QPushButton[text="OK"] {
    qproperty-text: "好的";
}
QMessageBox QPushButton[text="Open"] {
    qproperty-text: "打开";
}
QMessageBox QPushButton[text="Save"] {
    qproperty-text: "保存";
}
QMessageBox QPushButton[text="Cancel"] {
    qproperty-text: "取消";
}
QMessageBox QPushButton[text="Close"] {
    qproperty-text: "关闭";
}
QMessageBox QPushButton[text="Discard"] {
    qproperty-text: "不保存";
}
QMessageBox QPushButton[text="Don't Save"] {
    qproperty-text: "不保存";
}
QMessageBox QPushButton[text="Apply"] {
    qproperty-text: "应用";
}
QMessageBox QPushButton[text="Reset"] {
    qproperty-text: "重置";
}
QMessageBox QPushButton[text="Restore Defaults"] {
    qproperty-text: "恢复默认";
}
QMessageBox QPushButton[text="Help"] {
    qproperty-text: "帮助";
}
QMessageBox QPushButton[text="Save All"] {
    qproperty-text: "保存全部";
}
QMessageBox QPushButton[text="&Yes"] {
    qproperty-text: "是";
}
QMessageBox QPushButton[text="Yes to &All"] {
    qproperty-text: "全部都是";
}
QMessageBox QPushButton[text="&No"] {
    qproperty-text: "不";
}
QMessageBox QPushButton[text="N&o to All"] {
    qproperty-text: "全部都不";
}
QMessageBox QPushButton[text="Abort"] {
    qproperty-text: "终止";
}
QMessageBox QPushButton[text="Retry"] {
    qproperty-text: "重试";
}
QMessageBox QPushButton[text="Ignore"] {
    qproperty-text: "忽略";
}
"""


def wxqt_Process(sock_recv,sock_send,web_sock_recv,web_sock_send):
    qt_app = QApplication(sys.argv)
    qt_app.setStyleSheet(TextStyle)
    wx_app = wx.App()
    wx_window.MyFramelogin(None,sock_recv,sock_send,web_sock_recv,web_sock_send).Show()
    wx_app.MainLoop()
    qt_app.exec_()


class webhook_Process(Process):
    def __init__(self, daemon,sock_send,sock_recv,web_sock_send,web_sock_recv):    
        super(Process, self).__init__()
        self.daemon = daemon
        self.sock_send = sock_send
        self.sock_recv = sock_recv
        self.web_sock_send = web_sock_send
        self.web_sock_recv = web_sock_recv

    def run(self):
        ret = self.getret()
        if ret:
            hook = hook_socket.pyhookstart(self.sock_send)
            hook.start()
            hook_socket.web_send(self.web_sock_recv,ret).start()
            hook_socket.sok_send(self.sock_recv,hook).start()
            hook_socket.socketrun(self.web_sock_send,os.path.basename(sys.argv[0]))
        else:
            print("退出")

    def getret(self):
        websock = None
        while True:
            dic = self.sock_recv.get(True)
            if dic['type'] == 'next':
                setting.MYCONFIG.set_cfg("f_ip",dic['msg'])
                setting.MYCONFIG.ffffffffffffffff = dic['id']
                websock = hook_socket.pywebsocket(dic['msg'],self.web_sock_send)
                websock.start()
            elif dic['type'] == 'close':
                return False
            elif dic['type'] == 'login':
                return websock


if __name__ == "__main__":
    print(run_my)
    if run_my == False:
        freeze_support()
        sock_send = Queue()
        sock_recv = Queue()
        web_sock_send = Queue()
        web_sock_recv = Queue()
        webhook_Process(True,sock_send,sock_recv,web_sock_send,web_sock_recv).start()
        wxqt_Process(sock_send,sock_recv,web_sock_send,web_sock_recv)

